/*!
 @header OrderConfirmViewController.m
 @author 周益
 @version 1.0.0 2013/02/06 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "OrderConfirmViewController.h"
#import "Config.h"
#import "Helper.h"
#import "JSONRequest.h"
#import "OrderPickerViewController.h"
#import "ConfirmPayViewController.h"
#import "JMFBase.h"
#import "RTLabel.h"
#import "RegexKitLite.h"
#import "ASImageView.h"
#import "AddressPickerViewController.h"


//  -----------------------类详细介绍-----------------------
//  在确认订单时有跨圈子交易和区域范围交易的业务逻辑和地址设置的逻辑，
//
//  <功能描述>：跨圈子交易时，在确认订单时，会判断卖家是否在用户的圈子里面，如果没有在且购买金额小于卖家的担保金额，此时会弹出一个AlertView提示用户是否继续交易，否则继续交易。
//     <注意事项>：通过判断此商品的来源动态是否是推荐动态来确认是否是跨圈子交易，如果是推荐动态，那么就是跨圈子交易。
//
//  <功能描述>：区域范围交易，根据地址的包含逻辑来判断当前商品是否支持在用户的收获地址售卖。由于收获地址一定是详细地址，所以只需要判断收货地址的字符串是否包含
//     商品的售卖区域即可。
//
//  <功能描述>：地址设置。
//           1、当用户选中的配送方式是快递，那么地址view应显示收获地址，并且收获地址栏是可点击的，会PUSH到OrderPickerVC，让用户选择地址。此时当前view
//            会默认显示一个地址，显示优先级是：用户手动选择的地址 > 默认收获地址。用户在选择了一个地址后，会记录这个被选中的地址的ID，以便下次进入
//            OrderPickerVC时，会默认选中这个地址。如果用户没有设置过默认收获地址，那么此处就显示地址列表中第一个地址信息作为用户的收获地址。
//
//           2、当用户选中的配送方式是自取，那么地址view应显示取货地址，并且取货地址栏是不可点击的，地址栏显示的是卖家在设置商品时填写的取货地址信息。
//
//  -----------------------类升级日志-----------------------
//
//  修改人   修改时间        修改描述              版本
//
// 周益     13-9-5        增加物流模板            1.1.1


@interface OrderConfirmViewController ()

@end

@implementation OrderConfirmViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithDynamic:(NSDictionary *)dynamic
{
    self = [super init];
    if (self) {
        _dynamic = dynamic;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = _L(@"vc_title_007");
    
    //背景色
    _scrollView.backgroundColor = _SYS_COLOR_BACKGROUND;
    
    //获取一系列确认订单相关的信息
    //获取地址信息
    [self obtainConsigneeList];
    
    //导航按钮：返回
    UIButton* backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    _scrollView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, _SYS_FRAME_HEIGHT)];
    _scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_bg_012@2x" ofType:@"png"]]];
    _scrollView.customDelegate = self;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;    
    [_scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height + 180.0f)];
    [self.view addSubview:_scrollView];
    
    
    //订单主要信息栏
    _majorInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 190)];
    _majorInfoView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_majorInfoView];
    
    //卖家栏
    RTLabel *sellerLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 5, 300, 25)];
    sellerLabel.text = [NSString stringWithFormat:_L(@"tpl_sellername_01"), ([_dynamic[@"type"] intValue] == 2 ? _dynamic[@"creatorRealName"] : _dynamic[@"sellerRealName"])];
    sellerLabel.font = _SYS_FONT_005;
    [_majorInfoView addSubview:sellerLabel];
    
    //line
    UIImageView *lineImageView_1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, sellerLabel.frame.size.height + sellerLabel.frame.origin.y, 320, 0.5)];
    lineImageView_1.backgroundColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1.0f];
    [_majorInfoView addSubview:lineImageView_1];
    
    //商品名
    UILabel *productNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, lineImageView_1.frame.size.height + lineImageView_1.frame.origin.y + 5, 230, 20)];
    productNameLabel.textColor = _SYS_COLOR_002;
    productNameLabel.font = _SYS_FONT_004;
    [_majorInfoView addSubview:productNameLabel];
    
    //商品的价格，描述信息
    UILabel *productOtherInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, productNameLabel.frame.size.height + productNameLabel.frame.origin.y, 230, 15)];
    productOtherInfoLabel.textColor = _SYS_COLOR_002;
    productOtherInfoLabel.font = _SYS_FONT_001;
    [_majorInfoView addSubview:productOtherInfoLabel];
    
    //商品栏
    UIImageView *productImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320-45-20, lineImageView_1.frame.size.height + lineImageView_1.frame.origin.y + 5, 45, 45)];
    productImageView.backgroundColor = [UIColor clearColor];
    [_majorInfoView addSubview:productImageView];
                                           
    
    //line
    UIImageView *lineImageView_2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, productImageView.frame.size.height + productImageView.frame.origin.y + 5, 320, 0.5f)];
    lineImageView_2.backgroundColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1.0f];
    [_majorInfoView addSubview:lineImageView_2];
    
    //库存数量Label
    RTLabel *inventoryLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, lineImageView_2.frame.origin.y + lineImageView_2.frame.size.height + 5.0f, 150.0f, 20.0f)];
    inventoryLabel.font = _SYS_FONT_005;
    [_majorInfoView addSubview:inventoryLabel];
    
    //line
    UIImageView *lineImageView_3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, inventoryLabel.frame.size.height + inventoryLabel.frame.origin.y + 5, 320, 0.5f)];
    lineImageView_3.backgroundColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1.0f];
    [_majorInfoView addSubview:lineImageView_3];
    
    //购买数量
    UILabel *quantityLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, lineImageView_3.frame.origin.y + lineImageView_3.frame.size.height, 65, 30)];
    quantityLabel.text = _L(@"title_count_01");    //购买数量
    quantityLabel.font = _SYS_FONT_005;
    [_majorInfoView addSubview:quantityLabel];
    
    //数量
    _quantityValue = [[UITextField alloc] initWithFrame:CGRectMake(quantityLabel.frame.origin.x + quantityLabel.frame.size.width + 5, lineImageView_3.frame.origin.y + lineImageView_3.frame.size.height + 3, 50, 30)];
    _quantityValue.borderStyle = UITextBorderStyleRoundedRect;
    _quantityValue.keyboardType = UIKeyboardTypeDecimalPad;
    _quantityValue.returnKeyType = UIReturnKeyDone;
    _quantityValue.textColor = _SYS_COLOR_002;
    _quantityValue.delegate = self;
    _quantityValue.tag = 99;
    _quantityValue.text = @"1";
    [_quantityValue addTarget:self action:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];
    [_majorInfoView addSubview:_quantityValue];
    
    //单位
    UILabel *unitLabel = [[UILabel alloc] initWithFrame:CGRectMake(_quantityValue.frame.origin.x + _quantityValue.frame.size.width, lineImageView_3.frame.origin.y + lineImageView_3.frame.size.height, 55, 30)];
    unitLabel.font = _SYS_FONT_005;
    unitLabel.textColor = _SYS_COLOR_002;
    [_majorInfoView addSubview:unitLabel];
    
    //line
    UIImageView *lineImageView_4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, _quantityValue.frame.size.height + _quantityValue.frame.origin.y + 5, 320, 0.5f)];
    lineImageView_4.backgroundColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1.0f];
    [_majorInfoView addSubview:lineImageView_4];
    
    //商品金额
    _amountLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10.0f, lineImageView_4.frame.origin.y + lineImageView_4.frame.size.height + 5.0f, 140, 30)];
    _amountLabel.font = _SYS_FONT_005;
    [_majorInfoView addSubview:_amountLabel];
    
    
    //交货方式栏
    _transportationView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, _majorInfoView.frame.origin.y
                                                                          + _majorInfoView.frame.size.height + 10.0f, 320.0f, 100.0f)];
    _transportationView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_transportationView];
    
    
    _deliveryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 5.0f, 320, 25)];
    [_deliveryButton addTarget:self action:@selector(deliveryButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_transportationView addSubview:_deliveryButton];
    
    //交货方式文字
    UILabel *deliveryWayLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0.0f, 65, 30)];
    deliveryWayLabel.text = _L(@"title_method_01");   //交货方式
    deliveryWayLabel.font = _SYS_FONT_005;
    [_deliveryButton addSubview:deliveryWayLabel];
    
    //方式显示
    _wayLabel = [[UILabel alloc] initWithFrame:CGRectMake(310-100-20, 0.0f, 100, 30)];
    _wayLabel.font = _SYS_FONT_005;
    [_deliveryButton addSubview:_wayLabel];
    
    //箭头
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320- 20, 5, 10, 16)];
    arrowImageView.image = [UIImage imageNamed:@"com_icon_008"];
    [_deliveryButton addSubview:arrowImageView];
    
    //line
    _lineImageView_5 = [[UIImageView alloc] initWithFrame:CGRectMake(0, _deliveryButton.frame.size.height + _deliveryButton.frame.origin.y + 5, 320, 0.5f)];
    _lineImageView_5.backgroundColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1.0f];
    [_transportationView addSubview:_lineImageView_5];
        
    //地址
    _addressButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, _lineImageView_5.frame.origin.y + _lineImageView_5.frame.size.height + 10.0f, 320, 45)];
    [_addressButton addTarget:self action:@selector(addressButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_transportationView addSubview:_addressButton];
    
    _summaryLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5.0f, 70, 30)];
    _summaryLabel.text = _L(@"title_address_01");   //收货地址或取货地址
    _summaryLabel.font = _SYS_FONT_005;
    [_addressButton addSubview:_summaryLabel];
    
    //姓名与电话
    _nameAndMobileLabel = [[UILabel alloc] initWithFrame:CGRectMake(_summaryLabel.frame.origin.x + _summaryLabel.frame.size.width + 5, 0, 195, 15)];
    _nameAndMobileLabel.numberOfLines = 0;
    _nameAndMobileLabel.font = _SYS_FONT_003;
    [_addressButton addSubview:_nameAndMobileLabel];
    
    //详细地址
    _addressDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(_summaryLabel.frame.origin.x + _summaryLabel.frame.size.width + 5, _nameAndMobileLabel.frame.origin.y + _nameAndMobileLabel.frame.size.height + 5, 195, 15)];
    _addressDetailLabel.textColor = _SYS_COLOR_002;
    _addressDetailLabel.numberOfLines = 0;
    _addressDetailLabel.font = _SYS_FONT_003;
    [_addressButton addSubview:_addressDetailLabel];
    
    //箭头
    _arrowImageView_1 = [[UIImageView alloc] initWithFrame:CGRectMake(320- 20, 12, 10, 16)];
    _arrowImageView_1.image = [UIImage imageNamed:@"com_icon_008"];
    [_addressButton addSubview:_arrowImageView_1];
    
    //line
    _lineImageView_6 = [[UIImageView alloc] initWithFrame:CGRectMake(0, _addressButton.frame.size.height + _addressButton.frame.origin.y + 5, 320, 0.5)];
    _lineImageView_6.backgroundColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1.0f];
    [_transportationView addSubview:_lineImageView_6];

    //物流路线
    _pathLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10.0f, _lineImageView_6.frame.size.height + _lineImageView_6.frame.origin.y + 5, 250.0f, 20.0)];
    _pathLabel.font = _SYS_FONT_005;
    [_transportationView addSubview:_pathLabel];
    
    
    _lineImageView_7 = [[UIImageView alloc] initWithFrame:CGRectMake(0, _pathLabel.frame.size.height + _pathLabel.frame.origin.y + 5, 320, 0.5)];
    _lineImageView_7.backgroundColor = [UIColor colorWithRed:224/255.0f green:224/255.0f blue:224/255.0f alpha:1.0f];
    [_transportationView addSubview:_lineImageView_7];
    
    //物流运费
    _fareLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10.0f, _lineImageView_7.frame.size.height + _lineImageView_7.frame.origin.y + 5, 200.0f, 20.0)];
    _fareLabel.font = _SYS_FONT_005;
    _fareLabel.text = [NSString stringWithFormat:_L(@"tpl_36"), @""];
    [_transportationView addSubview:_fareLabel];
    
    //订单金额
    _orderAmountView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, _transportationView.frame.origin.y + _transportationView.frame.size.height + 10, 320.0f, 30.0f)];
    _orderAmountView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_orderAmountView];
    
    _orderAmountLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10.0f, 5.0f, 140, 30)];
    _orderAmountLabel.font = _SYS_FONT_005;
    _orderAmountLabel.text = [NSString stringWithFormat:_L(@"tpl_35"), 0];
    
    [_orderAmountView addSubview:_orderAmountLabel];    
    
    //给卖家留言栏
    _addtionView = [[UIView alloc] initWithFrame:CGRectMake(0, _orderAmountView.frame.origin.y + _orderAmountView.frame.size.height + 10, 320, 70)];
    _addtionView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_addtionView];
    
    //给卖家留言文本
    _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    _textLabel.text = _L(@"title_remark_01");      
    _textLabel.font = _SYS_FONT_005;
    
    [_addtionView addSubview:_textLabel];
    
    _remarkField = [[UITextField alloc] initWithFrame:CGRectMake(10, _textLabel.frame.origin.y + _textLabel.frame.size.height, 300, 30)];
    _remarkField.delegate = self;
    _remarkField.backgroundColor = _SYS_COLOR_004;
    _remarkField.borderStyle = UITextBorderStyleNone;
    _remarkField.placeholder = _L(@"title_remark_02");
    _remarkField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    _remarkField.returnKeyType = UIReturnKeyDone;
    _remarkField.font = _SYS_FONT_005;
    [_addtionView addSubview:_remarkField];
    
    //提交订单
    _commitButton = [[UIButton alloc] initWithFrame:CGRectMake(10, _addtionView.frame.origin.y + _addtionView.frame.size.height + 15, 300, 40)];
    [_commitButton setBackgroundImage:[UIImage imageNamed:@"com_btn_006_n"] forState:UIControlStateNormal];
    _commitButton.titleLabel.font = _SYS_FONT_009;
    [_commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_commitButton setTitle:_L(@"btn_yes_02") forState:UIControlStateNormal];
    [_commitButton addTarget:self action:@selector(commitButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:_commitButton];
    
    
    //开始渲染view
                                           
    //获得商品对象
    NSDictionary *object = [_dynamic objectForKey:@"product"];
    
    //获得图片
    NSArray *pictures = [object objectForKey:@"pictures"];
    
    //价格、单位和商品简介
    float price = [[object objectForKey:@"price"] floatValue];
    NSString *unit = [object objectForKey:@"unitName"];
    NSString *description = [object objectForKey:@"description"];
    
    //配送方式与地址信息
    NSNumber *deliveryMode = [object objectForKey:@"deliveryMode"];
    NSDictionary *consignee = [object objectForKey:@"consignee"];
    
//    //谁承担物流运费
//    _bearTheFreight = [[object objectForKey:@"bearTheFreight"] integerValue];
//    
//    //同城物流费用
//    _cityFreight = [[object objectForKey:@"cityFreight"] floatValue];
//    
//    //全国物流费用
//    _nationalFreight = [[object objectForKey:@"nationalFreight"] floatValue];
    
    //商品库存数量
    _inventory = [[object objectForKey:@"inventory"] doubleValue];
    
    
    productNameLabel.text = [object objectForKey:@"name"];
    productOtherInfoLabel.text = description;
    
    //取第一张为商品的展示图
    if (pictures.count != 0) {
        
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[pictures objectAtIndex:0] objectForKey:@"previewUrl"]]];
        productImageView.image = [UIImage imageWithData:imageData];
    }
    
    inventoryLabel.text = [NSString stringWithFormat:_L(@"tpl_inventory_01"), [NSString stringWithFormat:@"%.f%@", _inventory, unit]];
    unitLabel.text = [NSString stringWithFormat:@"/%@", unit];    //金额和单位
    _amountLabel.text = [NSString stringWithFormat:_L(@"title_amount_01"), price];
    
    //根据商品的配送方式，确定view如何显示
    //自取，无后续操作箭头
    if ([deliveryMode intValue] == 0) {
        
        //当前为自取模式
        _deliveryMode = NO;
        
        _wayLabel.text = _L(@"title_method_02");    //自取
        
        _summaryLabel.text = _L(@"title_address_03"); //取货地址
        
        //设置卖家的取货地址
        if (![consignee isKindOfClass:[NSNull class]]) {
            _nameAndMobileLabel.text = [NSString stringWithFormat:@"%@ %@", [consignee objectForKey:@"name"], [consignee objectForKey:@"mobile"]];
            
            _addressDetailLabel.text = [NSString stringWithFormat:@"%@%@", [consignee objectForKey:@"region"], [consignee objectForKey:@"address"]];
            _addressDetailLabel.text = [_addressDetailLabel.text stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
            
            [self positioningFrameWith:_nameAndMobileLabel.text and:_addressDetailLabel.text deliveryMode:NO];
        }
        
        //禁止选择取货地址
        _addressButton.enabled = NO;
        
        //禁止选择交货方式
        _deliveryButton.enabled = NO;
        [arrowImageView setHidden:YES];
        
        [_arrowImageView_1 setHidden:YES];
        
        [self hidePathViewAndFareView];
        
        //快递
    }else if ([deliveryMode intValue] == 1){
        
        //当前为快递模式
        _deliveryMode = YES;
        
        _wayLabel.text = _L(@"title_method_03");    //快递
        
        //禁止选择交货方式
        _deliveryButton.enabled = NO;
        [arrowImageView setHidden:YES];
        
        
        _summaryLabel.text = _L(@"title_address_01"); //收货地址
        
        //设置默认的收货地址
        [self defaultAddressOperaiton];
        
        //快递和自取
    }else{
        
        //当前为快递模式
        _deliveryMode = YES;
        
        _wayLabel.text = _L(@"title_method_03");    //快递
        
        //允许选择交货方式
        _deliveryButton.enabled = YES;
        [arrowImageView setHidden:NO];
        
        _summaryLabel.text = _L(@"title_address_01"); //收货地址
        
        //设置默认的收货地址
        [self defaultAddressOperaiton];
    }
    [self positioningFrameWith:_nameAndMobileLabel.text and:_addressDetailLabel.text deliveryMode:_deliveryMode];

    [self showProductAmount];
    
    [self freightAmountWithProduct:[_dynamic[@"product"][@"id"] integerValue] andCount:[[_quantityValue text] floatValue]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent
{
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

/*!
 @abstract 地址信息切换按钮事件
 */
- (void)addressButtonEvent:(id)sender
{
    AddressPickerViewController *addressPickerViewVC = [[AddressPickerViewController alloc] initWithSelectedSelectedAddrId:[_address[@"id"] integerValue]];
    addressPickerViewVC.delegate = self;

    [self.navigationController pushViewController:addressPickerViewVC animated:YES];
}

/*!
 @abstract 配送方式切换按钮事件
 */
- (void)deliveryButtonEvent:(id)sender
{
    OrderPickerViewController *orderPickerVC = [[OrderPickerViewController alloc] init];
    orderPickerVC.delegate = self;
    orderPickerVC.deliveryIsSelected = _wayLabel.text;
    [orderPickerVC isDelivery:YES];
    [orderPickerVC setWhichRowIsSelectedForDelivery:_deliveryIsSelectedIndexPath];
    [orderPickerVC.navigationItem setTitle:_L(@"title_method_01")];    //收货地址
    [self.navigationController pushViewController:orderPickerVC animated:YES];
}

/*!
 @abstract 确认订单按钮事件
 向服务器请求确认订单。
 */
- (void)commitButtonEvent:(id)sender
{    
    if (_wayLabel.text == nil || _wayLabel.text.length == 0) {
        [Helper showAlertWithTitle:nil andMessage:_L(@"title_method_04")];   //请选择配送方式
        return;
    }
    
    //获得商品对象
    NSDictionary *object = [_dynamic objectForKey:@"product"];
    
    if ([_wayLabel.text isEqualToString:_L(@"title_method_03")]    //快递
        && _addressDetailLabel.text.length == 0) {
        
        [Helper showAlertWithTitle:nil andMessage:_L(@"title_address_02")];  //请选择收货地址
        return;
        
    }else if ([_wayLabel.text isEqualToString:_L(@"title_method_03")]  //快递
              && _addressDetailLabel.text.length != 0){
        
        //商品销售区域
        NSString *salesRegion = [object objectForKey:@"salesRegion"];
        
        //除去收货地址和商品销售区域的空格
        NSString *consigneeRegion = [_addressDetailLabel.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *noWhitespaceRegion = [salesRegion stringByReplacingOccurrencesOfString:@" " withString:@""];
        
#warning 类和方法说明要注明此段。 --<< 该警告在注释写完前不得移除
        //销售区域只是全国那么就无视
        if (![noWhitespaceRegion isEqualToString:@"全国"]) {
            
            if (noWhitespaceRegion != nil) {
                
                //判断收货地址字符串是否包含商品的销售地址字符串，如果包含则表示收货地址范围是在销售地址的范围中的，则可以确认订单
                //否则不可以确认订单
                NSRange range = [consigneeRegion rangeOfString:noWhitespaceRegion];
                
                if (range.location == NSNotFound) {
                    
                    NSString *message = [NSString stringWithFormat:_L(@"tpl_sellsarea_01"), salesRegion];   //当前商品只在'%@'出售，无法购买
                    [Helper showAlertWithTitle:@"" andMessage:message];
                    return;
                }
            }
        }
    }
    
    NSInteger productId = [[object objectForKey:@"id"] integerValue];   //商品ID
    NSInteger quantity = [_quantityValue.text doubleValue];            //商品数量
    
    //当购买数量大于商品数量时，弹出提示框
    if(quantity > _inventory){
        [Helper showAlertWithTitle:@"" andMessage:_L(@"alert_44")];
        return;
    }
    
    
    NSInteger deliveryMode = 0;
    NSString *receivingId = nil;
    
    //如果是自取，deliveryMode为0，receivingId为空即可；
    if ([_L(@"title_method_02") isEqualToString:_wayLabel.text]) {   //自取
        
        deliveryMode = 0;
        receivingId = @"";
        
    }
    //如果是快递，deliveryMode为1
    else if([_L(@"title_method_03") isEqualToString:_wayLabel.text]) {    //快递
        
        deliveryMode = 1;
        if ([_address isEqual:[NSNull null]] || _address == nil) {
            receivingId = @"";
            
            [Helper showAlertWithTitle:@"" andMessage:_L(@"title_address_02")];
            
            return;
        }
        
        receivingId = [NSString stringWithFormat:@"%@" ,[_address objectForKey:@"id"]];
    }
    //如果是快递和自取，deliveryMode为2
    else{
        deliveryMode = 2;
        if ([_address isEqual:[NSNull null]] || _address == nil) {
            receivingId = @"";
            
            [Helper showAlertWithTitle:@"" andMessage:_L(@"title_address_02")];
            return;
        }
        receivingId = [NSString stringWithFormat:@"%@" ,[_address objectForKey:@"id"]];
    }
    
    //动态ID
    NSInteger dynamicId  = [[_dynamic objectForKey:@"id"] integerValue];

    _orderComfirmRequest = [[JSONRequest alloc] initWithURLString:_JSON_URL_ORDER_CONFIRM];
    [_orderComfirmRequest setValue:[NSString stringWithFormat:@"%d", productId]    forParameter:@"productId"];
    [_orderComfirmRequest setValue:[NSString stringWithFormat:@"%d", quantity]     forParameter:@"quantity"];
    [_orderComfirmRequest setValue:[NSString stringWithFormat:@"%d", deliveryMode] forParameter:@"deliveryMode"];
    [_orderComfirmRequest setValue:receivingId                                     forParameter:@"receivingId"];
    [_orderComfirmRequest setValue:[NSString stringWithFormat:@"%d", dynamicId]    forParameter:@"dynamicId"];
    [_orderComfirmRequest setValue:_remarkField.text                               forParameter:@"remark"];
    
    if (_deliveryMode) {
        [_orderComfirmRequest setValue:[NSString stringWithFormat:@"%.2f", _currentFreight] forParameter:@"freightAmount"];
        [_orderComfirmRequest setValue:_logisticsRoutes forParameter:@"logisticsRoutes"];
    }
    
    //当跨圈子交易时，如果买家购买商品的订单价格超过卖家的担保金，要提示用户是否继续交易
    //跨圈子，则只存在推荐动态里面
#warning 暂时将推荐动态作为是否是跨圈子交易的标识
    //此商品的卖家id
    
    NSString *sellerId = [_dynamic objectForKey:@"sellerId"];
    if (sellerId != nil && ![@"" isEqualToString:sellerId]) {
        
        //担保金
        NSString *amountOfGuarantee = [self amountOfGuaranteeRequest:sellerId];
       
        //订单价格
        float amount = [[[_dynamic objectForKey:@"product"] objectForKey:@"price" ] floatValue] * quantity;
        
        if (DynamicCategoryRecommend == [[_dynamic objectForKey:@"type"] integerValue] && [amountOfGuarantee floatValue] < amount) {
            
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:_L(@"alert_42") message:@"" delegate:self cancelButtonTitle:_L(@"btn_no_02") otherButtonTitles:_L(@"btn_yes_24"), nil];
            
            [alertView show];
            return;
        }
    }
    
    [self orderComfirmRequest];
}


#pragma mark - 私有方法
/*!
 @abstract 向服务器同步请求用户担保金
 */
- (NSString *)amountOfGuaranteeRequest:(NSString *)userId
{
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_U_GET_BASICINFO];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_01");

    [request setValue:userId forParameter:@"userId"];
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:[request synchronous]];
    if (jmf.status == JMStatusSucceed) {
        
        NSDictionary* dict = jmf.data;
        
        return [[dict objectForKey:@"sellerService"] objectForKey:@"amountOfGuarantee"];
        
    }
    else {
        [Helper easyShowJMFStatus:jmf];
        return nil;
    }
}

/*!
 @abstract 向服务器同步请求确认订单
 */
- (void)orderComfirmRequest
{
    NSDictionary *responseData = [_orderComfirmRequest synchronous];
    
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:responseData];
    if (jmf.status == JMStatusSucceed) {
        
        NSDictionary* dict = responseData[@"data"];
        
        NSMutableDictionary* order = [[NSMutableDictionary alloc] init];
        
        //设置请求参数
        [order setValue:dict[@"amount"] forKey:@"amount"];
        [order setValue:dict[@"orderId"] forKey:@"id"];
        [order setValue:_dynamic[@"product"] forKey:@"product"];
        
        ConfirmPayViewController* payVC = [[ConfirmPayViewController alloc] initWithOrder:order];
        payVC.dismissModal = true;
        payVC.delegate = self;
        
        UINavigationController* payNC = [[UINavigationController alloc] initWithRootViewController:payVC];

        [Helper navigationControllerCommonStyle:payNC];
        
        [self.navigationController presentModalViewController:payNC animated:true];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

/*!
 @abstract 同步请求用户的地址列表
 获得后如果地址列表为空就返回，如不为空那么就判断是否存在默认地址，如果存在的话，就选出来作为用户在确认订单时的首选项，
 如果没有则设置，则设置地址列表中第一个为确认订单时的首选项。
 */
- (void)obtainConsigneeList
{
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_CONSIGNEE_LIST];
    NSDictionary *responseData = [request synchronous];
    NSInteger status = [[responseData objectForKey:@"status"] integerValue];
    NSString *message = [responseData objectForKey:@"message"];
    
    if (status == 1) {
        
        NSArray *consigneeArray = [responseData objectForKey:@"data"];
        
        //地址列表为空，就返回
        if (consigneeArray.count == 0) {
            
            _defaultConsignee = nil;
            return;
        }
        
        //选取默认地址
        for (int i = 0 ; i < [consigneeArray count]; i++) {
            
            NSDictionary *consignee = [consigneeArray objectAtIndex:i];
            
            if ([[consignee objectForKey:@"status"] boolValue]) {
                
                _defaultConsignee = consignee;
                break;
            }
            
        }
        //如果没有设置过默认地址，那么就显示第一个
        
        if (_defaultConsignee == nil) {
            
            _defaultConsignee = [consigneeArray objectAtIndex:0];
        }
        
        //设置当前用户选择的地址
        _address = _defaultConsignee;
    }
    else {
        [Helper showAlertWithTitle:nil andMessage:message];
    }
}

/*!
 @abstract 设置过确认订单默认地址后，渲染在view上
 */
- (void)defaultAddressOperaiton
{
    if (_defaultConsignee) {

        [self renderAddressLabel:_defaultConsignee];
        
        //如果此地址不是默认地址，那么就标记它，在进入地址选择器后，让第一行被标记
        if (![[_defaultConsignee objectForKey:@"status"] boolValue]) {
            _consIdSelected = [NSNumber numberWithInt:[[_defaultConsignee objectForKey:@"id"] integerValue]];
        }
        
    }else{
        
        _addressDetailLabel.text = _L(@"title_address_02");    //请选择一个收货地址
    }
}

/*!
 @abstract 渲染地址Label
 */
- (void)renderAddressLabel:(NSDictionary *)address
{
    if (address != nil) {
        _nameAndMobileLabel.text = [NSString stringWithFormat:@"%@ %@", [address objectForKey:@"consignee"], [address objectForKey:@"mobile"]];
        _addressDetailLabel.text = [NSString stringWithFormat:@"%@ %@", [address objectForKey:@"region"], [address objectForKey:@"address"]];
        _addressDetailLabel.text = [_addressDetailLabel.text stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
    }
}

/*!
 @abstract 地址栏高度自适应时，需要对相关的view的frame进行调整，例如物流路线和物流运费的位置
 */
- (void)positioningFrameWith:(NSString *)firstLabelText and:(NSString *)secondLabelText deliveryMode:(BOOL)isExpressMode
{
    //姓名手机信息栏自适应后的大小
    CGSize size_1 = [firstLabelText sizeWithFont:_SYS_FONT_003 constrainedToSize:CGSizeMake(195.0f, 100.0f) lineBreakMode:NSLineBreakByWordWrapping];
    CGSize size_2 = [secondLabelText sizeWithFont:_SYS_FONT_003 constrainedToSize:CGSizeMake(195.0f, 100.0f) lineBreakMode:NSLineBreakByWordWrapping];

    //此时可能存在地址栏没有信息的情况，设置一个至少要保持的高度
    if(size_1.height < 15)
    {
        size_1.height = 15;
    }
    
    if(size_2.height < 15)
    {
        size_2.height = 15;
    }
    
    //如果选择后的收货地址的label自适应的高度大于原来的label的高度，那么_scrollview也要变化相应的高度
    _scrollView.contentSize = CGSizeMake(_scrollView.contentSize.width, _scrollView.contentSize.height + (size_1.height + size_2.height - _nameAndMobileLabel.frame.size.height - _addressDetailLabel.frame.size.height));

    _nameAndMobileLabel.frame = CGRectMake(_summaryLabel.frame.origin.x + _summaryLabel.frame.size.width + 5, 0.0f, 195.0f, size_1.height);
    
    _addressDetailLabel.frame = CGRectMake(_summaryLabel.frame.origin.x + _summaryLabel.frame.size.width + 5, _nameAndMobileLabel.frame.origin.y + _nameAndMobileLabel.frame.size.height + 5, 195, size_2.height);
    
    _addressButton.frame = CGRectMake(0.0f, _lineImageView_5.frame.origin.y + _lineImageView_5.frame.size.height + 10.0f, 320, size_1.height + size_2.height + 10.0f);
    
    
    //如果是快递的方式，那么此处将_transportationView的frame加高
    if (isExpressMode) {
        
        _lineImageView_6.frame = CGRectMake(0, _addressButton.frame.size.height + _addressButton.frame.origin.y + 5, 320, 0.5);
        
        _pathLabel.frame = CGRectMake(10.0f, _lineImageView_6.frame.size.height + _lineImageView_6.frame.origin.y + 5, 250.0f, 20.0);
        
        _lineImageView_7.frame = CGRectMake(0, _pathLabel.frame.size.height + _pathLabel.frame.origin.y + 5, 320, 0.5);
        
        _fareLabel.frame = CGRectMake(10.0f, _lineImageView_7.frame.size.height + _lineImageView_7.frame.origin.y + 5, 200.0f, 20.0);
        
        _transportationView.frame = CGRectMake(0.0f, _majorInfoView.frame.origin.y
                                               + _majorInfoView.frame.size.height + 10.0f, 320.0f, _fareLabel.frame.size.height + _fareLabel.frame.origin.y + 5);
        
    }
    
    else{
    
        _transportationView.frame = CGRectMake(0.0f, _majorInfoView.frame.origin.y
                                               + _majorInfoView.frame.size.height + 10.0f, 320.0f, _addressButton.frame.size.height + _addressButton.frame.origin.y + 5);
    }
    
    [self makeBetterViewPosition];
}

/*!
 @abstract 设置物流信息view中所以视图组件的alpha值
 */
- (void)setAlphaForPathViewAndFareView:(CGFloat)alpha
{
    _lineImageView_6.alpha = alpha;
    _pathLabel.alpha = alpha;
    _lineImageView_7.alpha = alpha;
    _fareLabel.alpha = alpha;
}

/*!
 @abstract 显示物流信息view
 */
- (void)showPathViewAndFareView
{
    [self setAlphaForPathViewAndFareView:1.0f];
    
    _scrollView.contentSize = CGSizeMake(_scrollView.contentSize.width, _scrollView.contentSize.height + 120.0f);
    [self makeBetterViewPosition];
}

/*!
 @abstract 隐藏物流信息view
 */
- (void)hidePathViewAndFareView
{
    [self setAlphaForPathViewAndFareView:0.0f];
    
    _scrollView.contentSize = CGSizeMake(_scrollView.contentSize.width, _scrollView.contentSize.height - 60.0f);
    [self makeBetterViewPosition];
}

/*!
 @abstract 调整订单金额view、给卖家留言view以及确认订单按钮的的frame
 这些views的frame要随着交货方式view的frame变化而变化
 */
- (void)makeBetterViewPosition
{    
    _orderAmountView.frame = CGRectMake(0.0f, _transportationView.frame.origin.y + _transportationView.frame.size.height + 10, 320.0f, 30.0f);
    
    _addtionView.frame = CGRectMake(0, _orderAmountView.frame.origin.y + _orderAmountView.frame.size.height + 10, 320, 70);
    
    _commitButton.frame = CGRectMake(10.0f, _addtionView.frame.origin.y + _addtionView.frame.size.height + 15, 300, 40);
}

///*!
// @abstract 得到地址信息的一级城市名称
// @param address 城市信息字符串，一般情况下，此字符串里包含的是卖家通过自定义的地址选择器（CustomPickerView）选择的地址信息，最多可包含
// 三级城市信息。
// @result 字符串类型，通常为一级城市名称。此城市名称用于显示在物流路线的收货城市，并且用于匹配计算物流运费。
// */
//- (NSString *)divideAddressString:(NSString *)address
//{
//    if (address == nil || address.length == 0) {
//        return @"";
//    }
//    
//    //直辖市取一级城市判断
//    //其它的取二级城市一起判断
//    NSArray *areaNameArray = [address componentsSeparatedByString:@" "];
//    
//    return [areaNameArray objectAtIndex:0];
//}
//
///*!
// @abstract 根据收货城市和发货城市来确定运费是同城运费的价格还是全国运费的价格
// @param shipCity    发货城市名称
// @param destination 收货城市名称
// */
//- (void)confirmFareShipCity:(NSString *)shipCity byDestination:(NSString *)destination
//{
//    
//    //首先要是买家承担运费，才有运费显示。否则就是免运费
//    //如果发货城市与收货城市一样，那么就选用同城运费的价格
//    //否则选择全国运费的价格
//    
//    //关于是否是同城的判断，直辖市只判断一级城市，非直辖市就要同时判断二级城市
//    
//    if (_bearTheFreight == 1) {
//        
//        NSArray *shipCityArray = [shipCity componentsSeparatedByString:@" "];
//        NSArray *destinationArray = [destination componentsSeparatedByString:@" "];
//        
//        //如果一级城市名称相同
//        if ([shipCityArray[0] isEqualToString:destinationArray[0]]) {
//            
//            //且是直辖市，那么就是同城
//            if ([self isMunicipalityDirectly:shipCityArray]) {
//                _currentFreight = _cityFreight;
//            }
//            
//            //且不是直辖市，那么就要判断二级城市。
//            //如果二级城市一样，那么就是同城，否则不是同城
//            else {
//                
//                //如果卖家在填写发货城市时，只选择了一级城市那么就是同城
//                if (shipCityArray.count == 1) {
//                    
//                    _currentFreight = _cityFreight;
//                    
//                }
//                
//                //同时存在发货地址和收货地址同时存在二级城市，那么就判断二级城市，一样就是同城，不一样就是全国
//                else if (shipCityArray.count > 1 && destinationArray.count > 1) {
//                    
//                    if ([shipCityArray[1] isEqualToString:destinationArray[1]]) {
//                        
//                        _currentFreight = _cityFreight;
//                    }
//                    else {
//                        
//                        _currentFreight = _nationalFreight;
//                    }
//                }
//                
//                //其它情况都是全国运费
//                else{
//                    _currentFreight = _nationalFreight;
//                }
//                
//            }
//        }
//        
//        //如果一级城市名称不相同，那就断定不是同城
//        else{
//            
//            _currentFreight = _nationalFreight;
//        }
//        
//        _fareLabel.text = [NSString stringWithFormat:_L(@"tpl_36"), [NSString stringWithFormat:@"%.2f元", _currentFreight]];
//        
//    }
//    
//    else{
//        _currentFreight = 0.0f;
//        _fareLabel.text = [NSString stringWithFormat:_L(@"tpl_36"), _L(@"title_71")];
//    }
//    
//    
//    
//}
//
//- (BOOL)isMunicipalityDirectly:(NSArray *)address
//{
//    if (address == nil || address.count == 0) {
//        
//        return NO;
//    }
//    
//    if ([MUNICIPALITY_DIRECTLY indexOfObject:address[0]] != NSNotFound) {
//        
//        return YES;
//    }
//    
//    return NO;
//}

/*!
 @abstract 设置订单金额：商品金额+物流运费
 */
- (void)showProductAmount
{
    float quantity = [[_quantityValue text] floatValue];
    _amount = [[[_dynamic objectForKey:@"product"] objectForKey:@"price" ] floatValue] * quantity;
    
    _amountLabel.text = [NSString stringWithFormat:_L(@"title_amount_01"), _amount];  //金额：￥%.2f

    _orderAmountLabel.text = [NSString stringWithFormat:_L(@"tpl_35"), _amount];
}

//向服务器  请求运费金额
- (void)freightAmountWithProduct:(NSInteger)productId andCount:(float)count
{
    //是自取模式，就不存在运费
    if (!_deliveryMode) {
        return;
    }
    
    JSONRequest* request = [[JSONRequest alloc] initWithURLString:_JSON_URL_ORDER_TRANSPORTATION_EXPENSES];
    request.delegate = self;
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_14");
    
    [request setValue:[NSNumber numberWithInteger:productId] forParameter:@"productId"];
    [request setValue:[NSNumber numberWithFloat:count] forParameter:@"count"];
    
    //如果没有选择过收货地址时，就采用默认地址
    
    
    //如果选择过收货地址时，就采用选择过的地址
    if (_address != nil) {
        
        [request setValue:_address[@"id"] forParameter:@"receivingId"];
    }
    else if (_defaultConsignee != nil && _consignee == nil) {
        [request setValue:_defaultConsignee[@"id"] forParameter:@"receivingId"];
    }
    
    //其它的情况不作处理
    else {
        return;
    }
    
    
    [request asynchronous];
}

#pragma mark - UITextField Delegate

- (void) textFieldDidChange:(UITextField *)textField
{
    if (textField.text.length == 0) {
        return;
    }
    
    if (! [textField.text isMatchedByRegex:@"^[0-9]+$"]) {
        [Helper showAlertWithTitle:nil andMessage:_L(@"alert_001")];  //产品价格请输入1-999的正整数
        if (textField.text.length > 0) {
            textField.text = [textField.text substringToIndex:textField.text.length - 1];
        }
        
        return;
    }
    
    if ([textField.text floatValue] <= 0) {
        [Helper showAlertWithTitle:nil andMessage:_L(@"alert_002")];   //您的购买数量应在1-999之间，请修改
        textField.text = @"1";
    }
    
    if ([textField.text floatValue] > 999) {
        [Helper showAlertWithTitle:nil andMessage:_L(@"alert_002")];   //您的购买数量应在1-999之间，请修改
        textField.text = @"999";
    }
    
    [self showProductAmount];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 99) {
        
        if (textField.text.length == 0) {
            [Helper showAlertWithTitle:nil andMessage:_L(@"alert_003")];   //请输入您的购买数量
            return false;
        }
        
        if ([textField.text floatValue] <= 0) {
            [Helper showAlertWithTitle:nil andMessage:_L(@"alert_004")];   //您的购买数量小于1，请修改
            return false;
        }
        [self showProductAmount];
    }
    
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag != 99) {
        
        _offset = _scrollView.contentOffset.y;
        [_scrollView setContentSize:CGSizeMake(_scrollView.contentSize.width, _scrollView.contentSize.height + (246-180))];//原始滑动距离增加键盘高度
        
        CGPoint pt = [textField convertPoint:CGPointMake(0, 0) toView:_scrollView];//把当前的textField的坐标映射到scrollview上
        
        if(_scrollView.contentOffset.y-pt.y+44<=0)//判断最上面不要去滚动
            
            [_scrollView setContentOffset:CGPointMake(0, pt.y-80) animated:YES];
    }
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag != 99) {
        
        //开始动画
        [UIView beginAnimations:nil context:nil];
        //设定动画持续时间
        [UIView setAnimationDuration:0.3];
        
        [_scrollView setContentSize:CGSizeMake(_scrollView.contentSize.width, _scrollView.contentSize.height - (246-180))];
        [_scrollView setContentOffset:CGPointMake(0, _offset) animated:YES];
        //动画结束
        [UIView commitAnimations];
        
    }
    else {
        
        //购买数量一变，马上就要重新计算物流运费
        [self freightAmountWithProduct:[_dynamic[@"product"][@"id"] integerValue] andCount:[textField.text floatValue]];
    }
    
}

#pragma mark - OrderPickerViewController Delegate

- (void)addressPicked:(NSDictionary *)consignee
{
    _address = consignee;
    
    [self renderAddressLabel:consignee];

    //收货地址一变，马上就要重新计算物流运费
    [self freightAmountWithProduct:[_dynamic[@"product"][@"id"] integerValue] andCount:[[_quantityValue text] floatValue]];
    
    [self positioningFrameWith:_nameAndMobileLabel.text and:_addressDetailLabel.text deliveryMode:_deliveryMode];
}

/*!
 @abstract 用户在选择了交货方式后，此方法被调用
 */
- (void)deliveryPicked:(NSString *)way andIndexPath:(NSIndexPath *)indexPath
{
    _deliveryIsSelectedIndexPath = indexPath;
    _wayLabel.text = way;
    
    _nameAndMobileLabel.text = @"";
    _addressDetailLabel.text = @"";
    
    if ([_L(@"title_method_02") isEqualToString:way]) {   //自取
        _summaryLabel.text = _L(@"title_address_03");   //取货地址
        
        //当前为自取模式
        _deliveryMode = NO;
        
        //获得商品对象
        NSDictionary *object = _dynamic[@"product"];
        NSDictionary *consignee = object[@"consignee"];
        NSString *name = consignee[@"name"];
        NSString *mobile = consignee[@"mobile"];
        NSString *address = consignee[@"address"];
        
        //将按钮失效，隐藏箭头
        _addressButton.enabled = NO;
        [_arrowImageView_1 setHidden:YES];
        
        _nameAndMobileLabel.text = [NSString stringWithFormat:@"%@ %@", name, mobile];
        _addressDetailLabel.text = [NSString stringWithFormat:@"%@", address];
    
        [self hidePathViewAndFareView];
        
        //自取时无运费
        _currentFreight = 0.0f;
    
        [self showProductAmount];
        
        
        
    }else{
        
        //当前为快递模式
        _deliveryMode = YES;
        
        _summaryLabel.text = _L(@"title_address_01");     //收货地址
        
        //启用按钮和箭头
        _addressButton.enabled = YES;
        [_arrowImageView_1 setHidden:NO];
        
        //如果在快递模式下选择过地址，然后切换到了自取，再切换到快递时，要显示到用户上次在快递模式下选择的地址
        if (_address != nil && _address.count != 0) {
            
            [self renderAddressLabel:_address];
            
            [self showPathViewAndFareView];
        }
        
        else{
            
            [self defaultAddressOperaiton];
            
            [self showPathViewAndFareView];
        }
        
        [self freightAmountWithProduct:[_dynamic[@"product"][@"id"] integerValue] andCount:[[_quantityValue text] floatValue]];
    }
    
    [self positioningFrameWith:_nameAndMobileLabel.text and:_addressDetailLabel.text deliveryMode:_deliveryMode];
}

#pragma mark - CloseController Delegate

- (void)closeViewController
{
    [self backButtonEvent];
}

#pragma mark - UIResponder Event

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_quantityValue resignFirstResponder];
    [_remarkField resignFirstResponder];
}

#pragma mark - UIAlertView Delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self orderComfirmRequest];

    }
}

#pragma mark - JSONRequest Delegate

- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        NSLog(@"%@", jmf.data[@"logisticsRoutes"]);
        
        //设置运费金额和物流路线  以及设置订单金额
        _logisticsRoutes = jmf.data[@"logisticsRoutes"];
        
        if (_logisticsRoutes != nil && _logisticsRoutes.length != 0) {
            _pathLabel.text = [NSString stringWithFormat:_L(@"tpl_37"), _logisticsRoutes];
        }
        else {
            _pathLabel.text = [NSString stringWithFormat:_L(@"tpl_37"), @""];
        }
        
        //设置运费，如果运费为0，就显示卖家承担运费
        if ([jmf.data[@"freightAmount"] floatValue] != 0.0f) {
            
            _currentFreight = [jmf.data[@"freightAmount"] doubleValue];
            _fareLabel.text = [NSString stringWithFormat:_L(@"tpl_36"), [NSString stringWithFormat:@"%.2f元", _currentFreight]];
        }
        else{
            _fareLabel.text = [NSString stringWithFormat:_L(@"tpl_36"), _L(@"title_71")];
        }
        
        float orderAmount = _amount + [jmf.data[@"freightAmount"] floatValue];
        
        _orderAmountLabel.text = [NSString stringWithFormat:_L(@"tpl_35"), orderAmount];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}

@end
